package com.phonetools.commonutils.regex

import android.text.InputFilter
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * 限制edittext只能输入英文、中文
 */
val enCnFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[a-zA-Z\u4e00-\u9fa5]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制edittext只能输入英文
 */
val enFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[a-zA-Z]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制edittext只能输入中文
 */
val cnFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[\u4e00-\u9fa5]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制edittext只能输入英文、中文、数字
 */
val enCnNumFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[a-zA-Z0-9\u4e00-\u9fa5]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制edittext只能输入英文、数字
 */
val enNumFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[a-zA-Z0-9]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制edittext只能输入中文、数字
 */
val cnNumFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern = Pattern.compile("[0-9\u4e00-\u9fa5]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制只能特殊符号
 */
val specialFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern =
        Pattern.compile("[\\\\`~!@#\\$%\\^&\\*\\(\\)\\-_=\\+\\[\\{\\]\\}\\|;:'\",<\\.>/\\?！￥…（）—【】‘；：”“’。，、？]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制中文、数字、特殊符号
 */
val cnNumSpecialFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern =
        Pattern.compile("[0-9\u4e00-\u9fa5\\\\`~!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\|\\{\\}':;\",\\[\\]\\.<>/\\?！￥…（）—【】‘；：”“’。，、？]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 限制英文、数字、特殊符号
 */
val enNumSpecialFilter = InputFilter { source, start, end, dest, dstart, dend ->
    val p: Pattern =
        Pattern.compile("[0-9a-zA-Z\\\\`~!@#\\$%\\^&\\*\\(\\)\\-_\\+=\\|\\{\\}':;\",\\[\\]\\.<>/\\?！￥…（）—【】‘；：”“’。，、？]+")
    val m: Matcher = p.matcher(source.toString())
    if (!m.matches()) "" else null
}

/**
 * 数字
 */
const val numRegex = "[0-9]+"

/**
 * 电话号码
 */
const val phoneRegex = "[0-9#*]+"

/**
 * 英文
 */
const val enRegex = "[a-zA-Z]+"

/**
 * 中文
 */
const val cnRegex = "[\u4e00-\u9fa5]+"

/**
 * 特殊符号
 */
const val specialRegex =
    "[\\\\`~!@#\\$%\\^&\\*\\(\\)\\-_=\\+\\[\\{\\]\\}\\|;:'\",<\\.>/\\?！￥…（）—【】‘；：”“’。，、？]+"

/**
 * 空格
 */
const val spaceRegex = "[ ]+"

/**
 * 支持多个regex，可输入(条件1)或(条件2)或(...)
 */
fun getRegexFilter(vararg regex: String): Array<InputFilter> {
    return arrayOf(
        InputFilter { source, start, end, dest, dstart, dend ->
            var ret: Boolean = false
            regex.map { Pattern.compile(it) }.forEachIndexed { index, pattern ->
                val m = pattern.matcher(source.toString())
                ret = if (index == 0) {
                    !m.matches()
                } else {
                    ret && !m.matches()
                }
            }
            if (ret) "" else null
        }
    )
}